

package com.hd2tecnologia.telas;

import com.hd2tecnologia.entidades.Cliente;
import com.hd2tecnologia.mensagens.LogErros;
import com.hd2tecnologia.modeloBanco.ClienteBD;
import com.hd2tecnologia.utilidades.Icones;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dulcyelliton Silva
 */
public class ClienteTela extends javax.swing.JFrame {
    
    
    private DefaultTableModel tableModel;
    private ListSelectionModel listModel;
    private List<Cliente> clientes;
    private static ClienteTela instance; //Utilizada para auxiliar na instancia do formulário
    
    Icones icone = new Icones();
    
    public ClienteTela() {
        try {
            initComponents();
            defineModelo();
            atualizaTabela();
        } catch (SQLException ex) {
            Logger.getLogger(ClienteTela.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

     
    
     private void defineModelo(){
        //O TABLEMODEL é utilizado para definir os dados da minha tabela tabela!
        tableModel = (DefaultTableModel) tbl_Clientes.getModel();
        //O  utilizado para escutar os eventos da minha tabela!
        listModel = tbl_Clientes.getSelectionModel();
        listModel.addListSelectionListener(new ListSelectionListener(){
           
            public void valueChanged(ListSelectionEvent e){
                if(!e.getValueIsAdjusting()){
                   mostraDetalheCliente();
                }
            }
        });
    }
     
     private void mostraDetalheCliente() {
        if (tbl_Clientes.getSelectedRow() != -1) {
            btn_EditarCliente.setEnabled(true);
            btn_ExcluirCliente.setEnabled(true);
            int indice = tbl_Clientes.getSelectedRow();
            txt_NomeCliente.setText(clientes.get(indice).getNome());
            String auxTipo;
            if(clientes.get(indice).getTipo()==1) {
                auxTipo = "P.Fisica";
            }else{ 
                auxTipo = "P.Juridica";
            }
            cbb_TipoCliente.setSelectedItem(auxTipo);
            txt_CPF_CNPJ.setText(clientes.get(indice).getCpf_Cnpj());
            txt_IE.setText(clientes.get(indice).getIe());
            txt_Rua.setText(clientes.get(indice).getEndereco());
            txt_Numero.setText(clientes.get(indice).getNumero());
            txt_Cep.setText(clientes.get(indice).getCep());
            txt_Bairro.setText(clientes.get(indice).getBairro());
            txt_Cidade.setText(clientes.get(indice).getCidade());
            txt_Estado.setText(clientes.get(indice).getUf());
            txt_Telefone1.setText(clientes.get(indice).getTelefone_1());
            txt_Telefone2.setText(clientes.get(indice).getTelefone_2());
            txt_Fax.setText(clientes.get(indice).getFax());
            txt_Email.setText(clientes.get(indice).getEmail());
        } else {
            txt_NomeCliente.setText("");
            cbb_TipoCliente.setSelectedItem("Selecione");
            txt_CPF_CNPJ.setText("");
            txt_IE.setText("");
            txt_Rua.setText("");
            txt_Numero.setText("");
            txt_Cep.setText("");
            txt_Bairro.setText("");
            txt_Cidade.setText("");
            txt_Estado.setText("");
            txt_Telefone1.setText("");
            txt_Telefone2.setText("");
            txt_Fax.setText("");
            txt_Email.setText("");
        }
    }
     
     
    public void atualizaTabela() throws SQLException {

        ClienteBD ModeloCliente = ClienteBD.getInstance();
        System.out.println(txt_PesquisaClienteNome.getText());
        
        if( txt_PesquisaClienteNome.getText().isEmpty() ){
            clientes = ModeloCliente.consultaCliente();
        }else{
            String nome = txt_PesquisaClienteNome.getText();
            clientes = ModeloCliente.consultaCliente(nome);   
        }
        
        //Util util = new Util();
        
        int numeroLinhas = tableModel.getRowCount();
        for (int i = 0; i < numeroLinhas; i++) {
            tableModel.removeRow(0);
        }
        for (int i = 0; i < clientes.size(); i++) {
            
            tableModel.insertRow(i, new Object[]{clientes.get(i).getId(), clientes.get(i).getNome(), clientes.get(i).getCpf_Cnpj(), clientes.get(i).getTelefone_1()});
        }         
    }
     
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txt_NomeCliente = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbb_TipoCliente = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txt_CPF_CNPJ = new javax.swing.JFormattedTextField();
        txt_IE = new javax.swing.JFormattedTextField();
        txt_Rua = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        txt_Numero = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        txt_Bairro = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        txt_Cidade = new javax.swing.JFormattedTextField();
        jLabel12 = new javax.swing.JLabel();
        txt_Estado = new javax.swing.JFormattedTextField();
        jLabel13 = new javax.swing.JLabel();
        txt_Telefone1 = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        txt_Telefone2 = new javax.swing.JFormattedTextField();
        jLabel15 = new javax.swing.JLabel();
        txt_Fax = new javax.swing.JFormattedTextField();
        jLabel16 = new javax.swing.JLabel();
        txt_Email = new javax.swing.JFormattedTextField();
        jLabel17 = new javax.swing.JLabel();
        txt_Cep = new javax.swing.JFormattedTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_Clientes = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btn_NovoCliente = new javax.swing.JButton();
        btn_InserirCliente = new javax.swing.JButton();
        btn_EditarCliente = new javax.swing.JButton();
        btn_SalvarEdicaoCliente = new javax.swing.JButton();
        btn_ExcluirCliente = new javax.swing.JButton();
        btn_CancelarEVoltar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        txt_PesquisaClienteNome = new javax.swing.JTextField();
        btn_BuscarCliente = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("[USUARIO] - DSControl - Sistema de Gestão de Comércio");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(51, 51, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Cadastro de Clientes");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Usuário"));

        jLabel2.setText("Nome:*");

        txt_NomeCliente.setEnabled(false);

        jLabel3.setText("CNPJ / CPF:*");

        jLabel4.setText("Tipo:*");

        cbb_TipoCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione", "P.Fisica", "P.Juridica" }));
        cbb_TipoCliente.setEnabled(false);
        cbb_TipoCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbb_TipoClienteActionPerformed(evt);
            }
        });

        jLabel6.setText("IE:*");

        jLabel7.setText("Rua:");

        try {
            txt_CPF_CNPJ.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("**************")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_CPF_CNPJ.setEnabled(false);
        txt_CPF_CNPJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_CPF_CNPJActionPerformed(evt);
            }
        });

        try {
            txt_IE.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("************")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_IE.setEnabled(false);

        try {
            txt_Rua.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("***********************************")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Rua.setEnabled(false);

        jLabel9.setText("Nº");

        try {
            txt_Numero.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("********")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Numero.setEnabled(false);

        jLabel10.setText("Bairro:");

        try {
            txt_Bairro.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("****************************************")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Bairro.setEnabled(false);

        jLabel11.setText("Cidade:");

        try {
            txt_Cidade.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("*********************************************")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Cidade.setEnabled(false);

        jLabel12.setText("UF:");

        try {
            txt_Estado.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("**")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Estado.setEnabled(false);

        jLabel13.setText("Tel. 1:");

        try {
            txt_Telefone1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(**)****-****")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Telefone1.setEnabled(false);

        jLabel14.setText("Tel. 2:");

        try {
            txt_Telefone2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(**)****-****")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Telefone2.setEnabled(false);

        jLabel15.setText("Fax:");

        try {
            txt_Fax.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(**)****-****")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Fax.setEnabled(false);

        jLabel16.setText("E-mail:");

        try {
            txt_Email.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("**********************************************")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Email.setEnabled(false);

        jLabel17.setText("Cep:");

        try {
            txt_Cep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("**.***-***")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txt_Cep.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel10)
                    .addComponent(jLabel13)
                    .addComponent(jLabel16)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_NomeCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txt_Telefone1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_Telefone2, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_Fax, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(cbb_TipoCliente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(19, 19, 19)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_CPF_CNPJ)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_IE, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txt_Email, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txt_Bairro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_Cidade, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_Estado, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txt_Rua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_Numero, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_Cep, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_NomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_CPF_CNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cbb_TipoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txt_IE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txt_Rua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_Cep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(txt_Numero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_Bairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(txt_Cidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(txt_Estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_Telefone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txt_Telefone2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_Fax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(txt_Email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cbb_TipoCliente, txt_CPF_CNPJ, txt_IE, txt_NomeCliente, txt_Numero, txt_Rua});

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txt_Fax, txt_Telefone1, txt_Telefone2});

        tbl_Clientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID / Número", "Nome", "CNPJ / CPF", "Telefone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbl_Clientes);
        tbl_Clientes.getColumnModel().getColumn(0).setMinWidth(70);
        tbl_Clientes.getColumnModel().getColumn(0).setMaxWidth(70);
        tbl_Clientes.getColumnModel().getColumn(2).setMinWidth(150);
        tbl_Clientes.getColumnModel().getColumn(2).setMaxWidth(150);
        tbl_Clientes.getColumnModel().getColumn(3).setMinWidth(120);
        tbl_Clientes.getColumnModel().getColumn(3).setMaxWidth(120);

        jPanel3.setLayout(new java.awt.GridLayout(1, 6, 2, 2));

        btn_NovoCliente.setText("Novo");
        btn_NovoCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_NovoClienteActionPerformed(evt);
            }
        });
        jPanel3.add(btn_NovoCliente);

        btn_InserirCliente.setText("Inserir");
        btn_InserirCliente.setEnabled(false);
        btn_InserirCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_InserirClienteActionPerformed(evt);
            }
        });
        jPanel3.add(btn_InserirCliente);

        btn_EditarCliente.setText("Editar");
        btn_EditarCliente.setEnabled(false);
        btn_EditarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_EditarClienteActionPerformed(evt);
            }
        });
        jPanel3.add(btn_EditarCliente);

        btn_SalvarEdicaoCliente.setText("Salvar");
        btn_SalvarEdicaoCliente.setEnabled(false);
        jPanel3.add(btn_SalvarEdicaoCliente);

        btn_ExcluirCliente.setText("Excluir");
        btn_ExcluirCliente.setEnabled(false);
        btn_ExcluirCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ExcluirClienteActionPerformed(evt);
            }
        });
        jPanel3.add(btn_ExcluirCliente);

        btn_CancelarEVoltar.setText("Cancelar");
        btn_CancelarEVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CancelarEVoltarActionPerformed(evt);
            }
        });
        jPanel3.add(btn_CancelarEVoltar);

        jLabel8.setText("Filtro por nome:");

        btn_BuscarCliente.setText("Buscar");
        btn_BuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_BuscarClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txt_PesquisaClienteNome, javax.swing.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btn_BuscarCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE))))
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txt_PesquisaClienteNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_BuscarCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-687)/2, (screenSize.height-547)/2, 687, 547);
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CancelarEVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CancelarEVoltarActionPerformed
        btn_InserirCliente.setEnabled(false);
        btn_EditarCliente.setEnabled(false);
        btn_ExcluirCliente.setEnabled(false);
        btn_SalvarEdicaoCliente.setEnabled(false);
        tbl_Clientes.setEnabled(true);
        desabilitarCampos();
        limparCampos();
        this.dispose();
    }//GEN-LAST:event_btn_CancelarEVoltarActionPerformed

    private void btn_NovoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_NovoClienteActionPerformed
        try {
            limparCampos();
            habilitarCampos();
            tbl_Clientes.clearSelection();
            atualizaTabela();
            tbl_Clientes.setEnabled(false);
            btn_InserirCliente.setEnabled(true);
            btn_EditarCliente.setEnabled(false);
            btn_ExcluirCliente.setEnabled(false);
            btn_SalvarEdicaoCliente.setEnabled(false);
        } catch (SQLException ex) {
            Logger.getLogger(ClienteTela.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_NovoClienteActionPerformed

    private void btn_InserirClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_InserirClienteActionPerformed
         if (txt_NomeCliente.getText().trim().equals("") || cbb_TipoCliente.getSelectedItem().toString().equals("Selecione") 
                || txt_CPF_CNPJ.getText().trim().equals("") || txt_IE.getText().trim().equals("") ) {
            JOptionPane.showMessageDialog(this, "Os Campos com * são obrigatórios!", "Erro", JOptionPane.ERROR_MESSAGE);
            
            // 01 -- Parte do código utilizado para verificar qual o campo obrigatório está faltando ser preenchido
            if(txt_NomeCliente.getText().trim().equals("")){
                txt_NomeCliente.requestFocus();
            }else{
                if(cbb_TipoCliente.getSelectedItem().toString().equals("Selecione")){
                    cbb_TipoCliente.requestFocus();
                }else{
                    if(txt_CPF_CNPJ.getText().trim().equals("")){
                       txt_CPF_CNPJ.requestFocus();
                    }else{
                        txt_IE.requestFocus();
                    }
                }
            }// 01 -- Parte do código utilizado para verificar qual o campo obrigatório está faltando ser preenchido
            
         }else{
            try {
                incluirUsuario();
            } catch (SQLException ex) {
                Logger.getLogger(ClienteTela.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
    }//GEN-LAST:event_btn_InserirClienteActionPerformed

    private void btn_BuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_BuscarClienteActionPerformed
        try {
            btn_EditarCliente.setEnabled(false);
            btn_SalvarEdicaoCliente.setEnabled(false);
            btn_ExcluirCliente.setEnabled(false);
            btn_InserirCliente.setEnabled(false);
            tbl_Clientes.setEnabled(true);
            limparCampos();
            desabilitarCampos();
            atualizaTabela();
        } catch (SQLException ex) {
            Logger.getLogger(ClienteTela.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_BuscarClienteActionPerformed

    private void btn_EditarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_EditarClienteActionPerformed
      habilitarCampos();
      tbl_Clientes.setEnabled(false);
      btn_SalvarEdicaoCliente.setEnabled(true);
    }//GEN-LAST:event_btn_EditarClienteActionPerformed

    private void btn_ExcluirClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ExcluirClienteActionPerformed
       int permissao = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluir o cliente selecionado?", "Confirmação de Exclusão", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION, icone.getLIXO());
     
     if(permissao == 0){
     ClienteBD clienteBD = ClienteBD.getInstance();
     LogErros log = LogErros.getInstance();
        try {
            if (tbl_Clientes.getSelectedRow() != -1) {
                int indice = tbl_Clientes.getSelectedRow();
                clienteBD.excluiCliente(clientes.get(indice));
                JOptionPane.showMessageDialog(null, "Usuário "+clientes.get(indice).getNome() +" excluído com Sucesso!", "Confirmação de Exclusão", JOptionPane.WARNING_MESSAGE, icone.getLIXO());
                atualizaTabela();
             }
        } catch (SQLException ex) {
            Logger.getLogger(ClienteTela.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
    
    }//GEN-LAST:event_btn_ExcluirClienteActionPerformed

    private void txt_CPF_CNPJActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_CPF_CNPJActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_CPF_CNPJActionPerformed

    private void cbb_TipoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbb_TipoClienteActionPerformed
           
    }//GEN-LAST:event_cbb_TipoClienteActionPerformed

   
    
    public void incluirUsuario() throws SQLException{
        Cliente cliente = Cliente.getInstance();
       
        cliente.setNome(txt_NomeCliente.getText());
        cliente.setEndereco(txt_Rua.getText());
        cliente.setBairro(txt_Bairro.getText());
        cliente.setCidade(txt_Cidade.getText());
        cliente.setUf(txt_Estado.getText());
        cliente.setCep(txt_Cep.getText());
        cliente.setTelefone_1(txt_Telefone1.getText());
        cliente.setEmail(txt_Email.getText());
        cliente.setNumero(txt_Numero.getText());
        cliente.setComplemento(null);
        cliente.setFax(txt_Fax.getText());
        cliente.setTelefone_2(txt_Telefone2.getText());
                
        if(cbb_TipoCliente.getSelectedItem()=="P.Fisica"){
            cliente.setTipo(1);
            cliente.setCpf_Cnpj(txt_CPF_CNPJ.getText());
            cliente.setIe("ISENTO");
        }else if(cbb_TipoCliente.getSelectedItem()=="P.Juridica"){
            cliente.setTipo(2);
            cliente.setCpf_Cnpj(txt_CPF_CNPJ.getText());            
            cliente.setIe(txt_IE.getText());
        }
        /*Para setar a senha do usupario, estou convertendo o JPasswordField que é um array de char para tipo String*/
        
        ClienteBD clienteBD = new ClienteBD();
        boolean aux;
        aux = clienteBD.incluiCliente(cliente);
        if(aux==true){
            limparCampos();
            desabilitarCampos();
            atualizaTabela();
            btn_InserirCliente.setEnabled(false);
            JOptionPane.showMessageDialog(null, "Cliente cadastrado com sucesso!");
        }else{
            
            //TESTE PARA VERIFICAR SE OS VALORES ESTÃO SENDO PASSADOS CORRETAMENTE
            System.out.println("NOME: "+cliente.getNome());
            System.out.println("BAIRRO: "+cliente.getBairro());
            System.out.println("CEP: "+cliente.getCep());
            System.out.println("CIDADE: "+cliente.getCidade());
            System.out.println("COMPLEMENTO: "+cliente.getComplemento());
            System.out.println("CPF: "+cliente.getCpf_Cnpj());
            System.out.println("EMAIL: "+cliente.getEmail());
            System.out.println("ENDERECO/RUA: "+cliente.getEndereco());
            System.out.println("FAX: "+cliente.getFax());
            System.out.println("ID: "+cliente.getId());
            System.out.println("IE: "+cliente.getIe());
            System.out.println("NUMERO: "+cliente.getNumero());
            System.out.println("TELEFONE 1: "+cliente.getTelefone_1());
            System.out.println("TELEFONE 2: "+cliente.getTelefone_2());
            System.out.println("TIPO: "+cliente.getTipo());
            System.out.println("ESTADO: "+cliente.getUf());
            
            
            limparCampos();
            desabilitarCampos();
            JOptionPane.showMessageDialog(null, "Erro ao cadastrar o cliente! Tente novamente.");
        }
    }
    
    
    public void habilitarCampos(){
        txt_NomeCliente.setEnabled(true);
        cbb_TipoCliente.setEnabled(true);
        txt_Rua.setEnabled(true);
        txt_Numero.setEnabled(true);
        txt_Cep.setEnabled(true);
        txt_Bairro.setEnabled(true);
        txt_Cidade.setEnabled(true);
        txt_Estado.setEnabled(true);
        txt_Telefone1.setEnabled(true);
        txt_Telefone2.setEnabled(true);
        txt_Fax.setEnabled(true);
        txt_Email.setEnabled(true);
        txt_CPF_CNPJ.setEnabled(true);
        txt_IE.setEnabled(true);
    }
    
    public void desabilitarCampos(){
        txt_NomeCliente.setEnabled(false);
        txt_Rua.setEnabled(false);
        txt_Bairro.setEnabled(false);
        txt_Cidade.setEnabled(false);
        txt_Numero.setEnabled(false);
        txt_Estado.setEnabled(false);
        txt_Cep.setEnabled(false);
        txt_Telefone1.setEnabled(false);
        txt_Telefone2.setEnabled(false);
        txt_Fax.setEnabled(false);
        txt_Email.setEnabled(false);
        cbb_TipoCliente.setEnabled(false);
        txt_CPF_CNPJ.setEnabled(false);
        txt_IE.setEnabled(false);
    }
    
    public void limparCampos(){
        txt_NomeCliente.setText("");
        txt_Rua.setText("");
        txt_Bairro.setText("");
        txt_Cidade.setText("");
        txt_Numero.setText("");
        txt_Estado.setText("");
        txt_Cep.setText("");
        txt_Telefone1.setText("");
        txt_Telefone2.setText("");
        txt_Fax.setText("");
        txt_Email.setText("");
        cbb_TipoCliente.setSelectedItem("Selecione");
        txt_CPF_CNPJ.setText(null);
        txt_IE.setText("");
    }
    
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UsuarioTela().setVisible(true);
            }
        });
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_BuscarCliente;
    private javax.swing.JButton btn_CancelarEVoltar;
    private javax.swing.JButton btn_EditarCliente;
    private javax.swing.JButton btn_ExcluirCliente;
    private javax.swing.JButton btn_InserirCliente;
    private javax.swing.JButton btn_NovoCliente;
    private javax.swing.JButton btn_SalvarEdicaoCliente;
    private javax.swing.JComboBox cbb_TipoCliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tbl_Clientes;
    private javax.swing.JFormattedTextField txt_Bairro;
    private javax.swing.JFormattedTextField txt_CPF_CNPJ;
    private javax.swing.JFormattedTextField txt_Cep;
    private javax.swing.JFormattedTextField txt_Cidade;
    private javax.swing.JFormattedTextField txt_Email;
    private javax.swing.JFormattedTextField txt_Estado;
    private javax.swing.JFormattedTextField txt_Fax;
    private javax.swing.JFormattedTextField txt_IE;
    private javax.swing.JTextField txt_NomeCliente;
    private javax.swing.JFormattedTextField txt_Numero;
    private javax.swing.JTextField txt_PesquisaClienteNome;
    private javax.swing.JFormattedTextField txt_Rua;
    private javax.swing.JFormattedTextField txt_Telefone1;
    private javax.swing.JFormattedTextField txt_Telefone2;
    // End of variables declaration//GEN-END:variables


public static ClienteTela getInstance(){
    if(instance==null){
        instance = new ClienteTela();
    }
    return instance;
}

public static void kilInstance(){
    instance = null;
}
    

}
